package cn.m3a1.creditsystem.service;

import cn.m3a1.creditsystem.bean.*;
import cn.m3a1.creditsystem.dao.*;
import cn.m3a1.creditsystem.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wyj
 * @date 2020/2/12
 * @time 下午3:29
 */
@Service
@Transactional
public class StudentResultService {
    @Autowired
    StudentResultMapper studentResultMapper;
    @Autowired
    StudentScheduleModelMapper studentScheduleModelMapper;
    @Autowired
    SchoolScheduleModelMapper schoolScheduleModelMapper;
    @Autowired
    SchoolScheduleMapper scheduleMapper;
    @Autowired
    StudentScheduleMapper studentScheduleMapper;
    @Autowired
    TrainingPlanMapper trainingPlanMapper;

    //    id
//    student_id 学号
//    student_name 姓名
//    subject 科目
//    student_grade 成绩
    public Boolean inserts(List<List<String>> list) {

        for (int i = 2; i < list.size(); i++) {
            List<String> lo = list.get(i);
            for (int j = 13; j < lo.size(); j++) {
                StudentResult studentResult = new StudentResult();
                studentResult.setStudentId(lo.get(0));
                studentResult.setStudentName(lo.get(1));
                String str = list.get(1).get(j);
                String[] strs = str.split("\\[");
                studentResult.setSubject(strs[0]);
                studentResult.setStudentGrade(Double.parseDouble(lo.get(j)));
                studentResultMapper.insert(studentResult);

            }
        }
        return true;
    }

    public List<StudentResult> studentResults(StudentResult studentResult, boolean ischeck) {
        StudentResultExample studentResultExample = new StudentResultExample();
        StudentResultExample.Criteria criteria = studentResultExample.createCriteria();
        if (studentResult.getStudentId() != null) {
            criteria.andStudentIdLike("%" + studentResult.getStudentId().trim() + "%");
        }
        if (studentResult.getStudentName() != null) {
            criteria.andStudentNameLike("%" + studentResult.getStudentName().trim() + "%");
        }
        if (studentResult.getSubject() != null) {
            criteria.andSubjectLike("%" + studentResult.getSubject().trim() + "%");
            //System.out.println(studentResult.getSubject());
        }
        if (ischeck) {
            criteria.andStudentGradeLessThan(60.0);
        }
        List<StudentResult> studentResults = studentResultMapper.selectByExample(studentResultExample);
        return studentResults;

    }

    public List<StudentResult> getLessResult(StudentResult studentResult) {
        StudentResultExample studentResultExample = new StudentResultExample();
        StudentResultExample.Criteria criteria = studentResultExample.createCriteria();
        if (studentResult.getStudentId() != null) {
            criteria.andStudentIdLike("%" + studentResult.getStudentId().trim() + "%");
        }
        if (studentResult.getStudentName() != null) {
            criteria.andStudentNameLike("%" + studentResult.getStudentName().trim() + "%");
        }
        if (studentResult.getSubject() != null) {
            criteria.andSubjectLike("%" + studentResult.getSubject().trim() + "%");
        }
        criteria.andStudentGradeLessThan(60.0);

        List<StudentResult> studentResults = studentResultMapper.selectByExample(studentResultExample);
        return studentResults;

    }

    public List<SchoolSchedule> getRebuid(int id) {

        StudentResult studentResult = studentResultMapper.selectByPrimaryKey(id);

        StudentScheduleExample studentScheduleExample = new StudentScheduleExample();
        StudentScheduleExample.Criteria criteria = studentScheduleExample.createCriteria();
        criteria.andStudentIdLike(studentResult.getStudentId());
        List<StudentSchedule> studentSchedules = studentScheduleMapper.selectByExample(studentScheduleExample);

        TrainingPlanExample trainingPlanExample = new TrainingPlanExample();
        TrainingPlanExample.Criteria criteria1 = trainingPlanExample.createCriteria();
        criteria1.andGradeYearEqualTo(studentSchedules.get(0).getGradeYear());
        criteria1.andProfessionalTitleEqualTo(studentSchedules.get(0).getProfessionalTitle());
        criteria1.andCourseNameEqualTo(studentResult.getSubject());
        List<TrainingPlan> trainingPlans = trainingPlanMapper.selectByExample(trainingPlanExample);

        String courseCode = trainingPlans.get(0).getCourseCode();
        SchoolScheduleExample scheduleExample = new SchoolScheduleExample();
        SchoolScheduleExample.Criteria criteria2 = scheduleExample.createCriteria();
        criteria2.andCourseCodeEqualTo(courseCode);
        List<SchoolSchedule> schoolSchedules = scheduleMapper.selectByExample(scheduleExample);


        StudentScheduleModelExample studentScheduleModelExample = new StudentScheduleModelExample();
        StudentScheduleModelExample.Criteria criteria3 = studentScheduleModelExample.createCriteria();
        criteria3.andStudentIdEqualTo(studentResult.getStudentId());
        List<StudentScheduleModel> studentScheduleModels = studentScheduleModelMapper.selectByExample(studentScheduleModelExample);

        List<SchoolSchedule> schedules = new ArrayList<>();
        for (SchoolSchedule schoolSchedule : schoolSchedules
                ) {
            SchoolScheduleModelExample schoolScheduleModelExample = new SchoolScheduleModelExample();
            SchoolScheduleModelExample.Criteria criteria4 = schoolScheduleModelExample.createCriteria();
            criteria4.andSchoolScheduleIdEqualTo(schoolSchedule.getId());
            List<SchoolScheduleModel> list = schoolScheduleModelMapper.selectByExample(schoolScheduleModelExample);
            a:for (int i =0; i<list.size();i++){
                for (int j =0; j<studentScheduleModels.size();j++) {
                    if (list.get(i).getWeek().equals(studentScheduleModels.get(j).getWeek())&&
                            list.get(i).getWeekDay().equals(studentScheduleModels.get(j).getWeekDay())&&
                            list.get(i).getDayTime().equals(studentScheduleModels.get(j).getDayTime())){
                        break a;
                    }
                    if (i==list.size()-1&&j==studentScheduleModels.size()-1){
                        schedules.add(schoolSchedule);
                    }

                }
            }

        }
        return schedules;
    }

    public List<SchoolSchedule> getReplice(int id) {
        String zhuanye="";
        StudentResult studentResult = studentResultMapper.selectByPrimaryKey(id);
        List<SchoolSchedule> schedules1 = new ArrayList<>();

        StudentScheduleExample studentScheduleExample = new StudentScheduleExample();
        StudentScheduleExample.Criteria criteria = studentScheduleExample.createCriteria();
        criteria.andStudentIdLike(studentResult.getStudentId());
        List<StudentSchedule> studentSchedules = studentScheduleMapper.selectByExample(studentScheduleExample);

        TrainingPlanExample trainingPlanExample = new TrainingPlanExample();
        TrainingPlanExample.Criteria criteria1 = trainingPlanExample.createCriteria();
        criteria1.andGradeYearEqualTo(studentSchedules.get(0).getGradeYear());
        criteria1.andProfessionalTitleEqualTo(studentSchedules.get(0).getProfessionalTitle());
        criteria1.andCourseNameEqualTo(studentResult.getSubject());
        criteria1.andCourseNatureLike("%选修%");
        List<TrainingPlan> trainingPlans = trainingPlanMapper.selectByExample(trainingPlanExample);
        if (trainingPlans.size()==0){
            //System.out.println(trainingPlans.get(0).getCourseNature());
            //System.out.println("必修课");
            return new ArrayList<SchoolSchedule>();
        }
        Double mycredit =trainingPlans.get(0).getStudentCredit()*1.5;
        //System.out.println(mycredit);


        TrainingPlanExample trainingPlanExample7 = new TrainingPlanExample();
        TrainingPlanExample.Criteria criteria7 = trainingPlanExample7.createCriteria();
        criteria7.andCollegeLike("%理工%");
        if (trainingPlans.get(0).getProfessionalTitle().contains("计算机")){
            criteria7.andProfessionalTitleLike("%计算机%");
            zhuanye = "计科";

            //System.out.println(trainingPlans7.size());

        } else if (trainingPlans.get(0).getProfessionalTitle().contains("电")){
            criteria7.andProfessionalTitleLike("%电%");
            zhuanye = "电";
        }else if (trainingPlans.get(0).getProfessionalTitle().contains("物流")){
            criteria7.andProfessionalTitleLike("%物流%");
            zhuanye = "物流";
        }
        List<TrainingPlan> trainingPlans7 = trainingPlanMapper.selectByExample(trainingPlanExample7);
        //System.out.println(trainingPlans7.size());
        TrainingPlanExample trainingPlanExample8 = new TrainingPlanExample();
        TrainingPlanExample.Criteria criteria8 = trainingPlanExample8.createCriteria();
        criteria8.andGradeYearEqualTo(studentSchedules.get(0).getGradeYear());
        criteria8.andProfessionalTitleEqualTo(studentSchedules.get(0).getProfessionalTitle());
        List<TrainingPlan> trainingPlans8 = trainingPlanMapper.selectByExample(trainingPlanExample8);
        //System.out.println(trainingPlans8.size());
        for (int i = 0; i <trainingPlans8.size() ; i++) {
            for (int j = 0; j <trainingPlans7.size() ; j++) {

                if (trainingPlans7.get(j).getCourseCode().equals(trainingPlans8.get(i).getCourseCode())){
                    //System.out.println(trainingPlans7.get(i).getCourseCode()+" "+trainingPlans8.get(j).getCourseCode());
                    trainingPlans7.remove(j);

                    j--;


                }
            }
        }
        //System.out.println(trainingPlans7.size());
//        System.out.println(trainingPlans7);
        List<SchoolSchedule> schedules = new ArrayList<>();
        StudentScheduleModelExample studentScheduleModelExample = new StudentScheduleModelExample();
        StudentScheduleModelExample.Criteria criteria3 = studentScheduleModelExample.createCriteria();
        criteria3.andStudentIdEqualTo(studentResult.getStudentId());
        List<StudentScheduleModel> studentScheduleModels = studentScheduleModelMapper.selectByExample(studentScheduleModelExample);
        for (int i = 0; i <trainingPlans7.size() ; i++) {
            String courseCode = trainingPlans7.get(i).getCourseCode();
            SchoolScheduleExample scheduleExample = new SchoolScheduleExample();
            SchoolScheduleExample.Criteria criteria2 = scheduleExample.createCriteria();
            criteria2.andCourseCodeEqualTo(courseCode);
            criteria2.andCollegeLike("%理工%");
            criteria2.andClassInformationLike("%"+zhuanye+"%");
            List<SchoolSchedule> schoolSchedules = scheduleMapper.selectByExample(scheduleExample);


            for (SchoolSchedule schoolSchedule : schoolSchedules
                    ) {
                SchoolScheduleModelExample schoolScheduleModelExample = new SchoolScheduleModelExample();
                SchoolScheduleModelExample.Criteria criteria4 = schoolScheduleModelExample.createCriteria();
                criteria4.andSchoolScheduleIdEqualTo(schoolSchedule.getId());
                List<SchoolScheduleModel> list = schoolScheduleModelMapper.selectByExample(schoolScheduleModelExample);
                a:for (int i1=0; i1<list.size();i1++){
                    for (int j =0; j<studentScheduleModels.size();j++) {
                        if (list.get(i1).getWeek().equals(studentScheduleModels.get(j).getWeek())&&
                                list.get(i1).getWeekDay().equals(studentScheduleModels.get(j).getWeekDay())&&
                                list.get(i1).getDayTime().equals(studentScheduleModels.get(j).getDayTime())){
                            break a;
                        }
                        if (i1==list.size()-1&&j==studentScheduleModels.size()-1){
                            //System.out.println("执行了....");
                            if (schoolSchedule.getCredit()>=mycredit){
                                schedules1.add(schoolSchedule);
                            }else{
                                schedules.add(schoolSchedule);
                            }

                        }

                    }
                }

            }
        }

        MyUtil myUtil =new MyUtil();
        for (int i = 0; i < schedules.size(); i++) {
            for (int j = 0; j < schedules.size(); j++) {
                if (myUtil.CompareSchoolSchedule(schedules.get(i),schedules.get(j))){
                    SchoolSchedule schedule =new SchoolSchedule();
                    schedule.setCourseCode(schedules.get(i).getCourseCode()+"+"+schedules.get(j).getCourseCode());
                    schedule.setCourseName(schedules.get(i).getCourseName()+"+"+schedules.get(j).getCourseName());
                    schedule.setTeacherName(schedules.get(i).getTeacherName()+"+"+schedules.get(j).getTeacherName());
                    schedule.setTeacherId(schedules.get(i).getTeacherId()+"+"+schedules.get(j).getTeacherId());
                    schedule.setTime(schedules.get(i).getTime()+"+"+schedules.get(j).getTime());
                    schedule.setClassInformation(schedules.get(i).getClassInformation()+"+"+schedules.get(j).getClassInformation());
                    schedule.setCredit(schedules.get(i).getCredit()+schedules.get(j).getCredit());
                    schedules1.add(schedule);

                }
            }
        }
        return  schedules1;
    }






    public List<TrainingPlan> getTrain(int id) {
        String zhuanye="";
        StudentResult studentResult = studentResultMapper.selectByPrimaryKey(id);

        StudentScheduleExample studentScheduleExample = new StudentScheduleExample();
        StudentScheduleExample.Criteria criteria = studentScheduleExample.createCriteria();
        criteria.andStudentIdLike(studentResult.getStudentId());
        List<StudentSchedule> studentSchedules = studentScheduleMapper.selectByExample(studentScheduleExample);

        TrainingPlanExample trainingPlanExample = new TrainingPlanExample();
        TrainingPlanExample.Criteria criteria1 = trainingPlanExample.createCriteria();
        criteria1.andGradeYearEqualTo(studentSchedules.get(0).getGradeYear());
        criteria1.andProfessionalTitleEqualTo(studentSchedules.get(0).getProfessionalTitle());
        criteria1.andCourseNameEqualTo(studentResult.getSubject());
        List<TrainingPlan> trainingPlans = trainingPlanMapper.selectByExample(trainingPlanExample);
        return  trainingPlans;
    }
}
